﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CarController : ControllerBase
    {
        private readonly ApiDBContext db;
        public CarController(ApiDBContext context)
        {
            db = context;
        }
        [HttpGet]
        public List<Car> GetAll()
        {
            return db.Car.OrderByDescending(s => s.id).ToList();
        }
        [HttpGet("{id}")]
        public Car? GetCarById(Guid id)
        {
            Car? car = new Car();
            car = db.Car.Where(s => s.id == id).FirstOrDefault();
            return car;
        }
        [HttpPost]
        public bool Add(Car car)
        {
            db.Car.Add(car);
            int rousult = db.SaveChanges();
            return rousult > 0 ? true : false;
        }
        [HttpPost]
        public bool Update(Car car)
        {
            db.Car.Update(car);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }
        [HttpDelete]
        public bool Delete(Guid ID)
        {
            Car? car = new Car();
            car = db.Car.Where(s => s.id == ID).FirstOrDefault();
            int result = 0;
            if (car != null)
            {
                car.DelTime = DateTime.Now;
                db.Car.Update(car);
                result = db.SaveChanges();
            }
            return result > 0 ? true : false;
        }
    }
}
